package org.thingsboard.server.dao.jpa.imp;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.thingsboard.server.dao.jpa.TGateway;
import org.thingsboard.server.dao.util.SqlDao;

import java.util.List;

@SqlDao
public interface GatewayRepository extends CrudRepository<TGateway, String> {
    TGateway findByGatewayCode(String gatewayCode);

    //通过边缘代理获取网关code
    @Query("SELECT b FROM TGateway b WHERE b.gatewayId =(SELECT parentId from TGateway a WHERE a.gatewayCode= :gatewayCode)")
    TGateway findGateway(@Param("gatewayCode")String gatewayCode);
    /**
     * 根据网关获取网关下面的所有边缘代理
     * @param gatewayCode
     * @return
     */
    @Query("SELECT a FROM TGateway a WHERE a.parentId=(SELECT gatewayId FROM TGateway WHERE gatewayCode= :gatewayCode)")
     List<TGateway> findAgents(@Param("gatewayCode")String gatewayCode);


    /**
     * 获取所有网关的gatewayCode
     * @return
     */
    @Query("SELECT a FROM TGateway a WHERE a.parentId is null ")
    List<TGateway> findGateways();
}
